package com.youth.custom.tag;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.tags.RequestContextAwareTag;

// import com.handpay.framework.cache.client.exception.CacheClientException;

/**
 * @ClassName: PropertisTag
 * @Description: TODO
 * @author yjing
 * @email yu.jing@99wuxian.com
 */
public class PropertisTag extends RequestContextAwareTag {
	private static final long serialVersionUID = 5726898396619748885L;
	private static final String INPUT_TYPE_TEXT = "text";
	private static final String INPUT_TYPE_RADIO = "radio";
	private static final String INPUT_TYPE_CHECKBOX = "checkbox";
	private static final String INPUT_TYPE_SELECT = "select";
	private static final String EVENT_ON_CHANGE = "onchange";
	private static final String EVENT_ON_CLICK = "onclick";

	private String groupCode;
	private String inputType;// text radio checkbox select
	private String cssClass;// 对应class
	private String selectedValues;// 选中值，以逗号分隔
	private String inputId;
	private String inputName;
	private Integer hasDefault;// 如果为1则展示默认第一个空值
	private String defaultName;// 用于下拉框
	private String defaultValue;// 用于下拉框

	private String styleClass;// 对应style
	private Integer isMultiple;

	public Integer getIsMultiple() {
		return isMultiple;
	}

	public void setIsMultiple(Integer isMultiple) {
		this.isMultiple = isMultiple;
	}

	private String onChange;
	private String onClick;
	private String[] selectedVals;

	private Integer isul;// checkbox radio
	private String ulcss;// checkbox radio
	private String ulstyle;// checkbox radio
	private String licss;
	private String listyle;
	private Integer txtPosition;// checkbox radio 文本位置，默认靠前，值1靠前，2靠后

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.tags.RequestContextAwareTag#
	 * doStartTagInternal()
	 */
	@Override
	// protected int doStartTagInternal() throws CacheClientException, IOException {
	protected int doStartTagInternal() throws IOException {
		this.init();
		String result = "";
		Map<String, Object> mapCache = new HashMap<String, Object>();
		// PropertisCacheTools cache = (PropertisCacheTools) this.getRequestContext().getWebApplicationContext().getBean("propertisCacheTools");
		if (this.inputType.equals(INPUT_TYPE_SELECT)) {
			result = createSelect(mapCache);
		} else if (this.inputType.equals(INPUT_TYPE_RADIO) || this.inputType.equals(INPUT_TYPE_CHECKBOX)) {
			result = createMulti(mapCache);
		} else if (this.inputType.equals(INPUT_TYPE_TEXT)) {
			result = createText(mapCache);
		}

		JspWriter out = pageContext.getOut();
		out.print(result);
		this.destroyParam();
		return TagSupport.EVAL_PAGE;
	}

	// private String createSelect(PropertisCacheTools cache) throws CacheClientException {
	private String createSelect(Map<String, Object> mapCache) {
		// List<PropertisOptions> pos = cache.getOptions(this.groupCode);
		PropertisOptions poption = new PropertisOptions();
		poption.setOptName("性别");
		poption.setOptValue("男");
		PropertisOptions poption2 = new PropertisOptions();
		poption2.setOptName("性别2");
		poption2.setOptValue("男2");
		List<PropertisOptions> pos = new ArrayList<PropertisOptions>();
		pos.add(poption);
		pos.add(poption2);
		
		
		StringBuffer sb = new StringBuffer();
		sb.append("性别<select id='" + this.inputId + "' name='" + this.inputName+"'");
		this.createBaseCss(sb, this.cssClass, this.styleClass);
		this.createEvent(sb);
		if (isMultiple > 0) {
			sb.append(" multiple='multiple' ");
		}
		sb.append(">");

		if (null == pos || pos.size() == 0) {
			sb.append("</select>");
		} else {
			if (this.hasDefault == 1) {
				sb.append("<option ");
				sb.append("value='" + this.defaultValue + "'>");
				sb.append(this.defaultName);
				sb.append("</option>");
			}

			for (PropertisOptions po : pos) {
				sb.append("<option ");
				if (null != selectedVals) {
					for (int i = 0; i < this.selectedVals.length; i++) {
						if (po.getOptValue().equals(this.selectedVals[i])) {
							sb.append("selected='selected'");
							break;
						}
					}
				}
				sb.append("value='" + po.getOptValue() + "'");
				sb.append(">");
				sb.append(po.getOptName());
				sb.append("</option>");
			}
			sb.append("</select>");
		}
		return sb.toString();
	}

	// private String createMulti(PropertisCacheTools cache) throws CacheClientException {
	private String createMulti(Map<String, Object> mapCache) {
		if (this.isul == 1) {
			return this.createMultiWithUl(mapCache);
		} else {
			return this.createMultiNomal(mapCache);
		}
	}

	// private String createMultiNomal(PropertisCacheTools cache) throws CacheClientException {
	private String createMultiNomal(Map<String, Object> mapCache) {
		// List<PropertisOptions> pos = cache.getOptions(this.groupCode);
		List<PropertisOptions> pos = new ArrayList<PropertisOptions>();
		StringBuffer sb = new StringBuffer();
		for (PropertisOptions po : pos) {
			if (this.txtPosition == 1) {
				sb.append(po.getOptName() + "  ");
			}
			sb.append("<input ");
			if (this.inputType.equals(INPUT_TYPE_RADIO)) {
				sb.append(" type='radio'");
			}
			if (this.inputType.equals(INPUT_TYPE_CHECKBOX)) {
				sb.append(" type='checkbox'");
			}
			sb.append(" id='" + this.inputId + "' name='" + this.inputName + "' ");
			if (null != selectedVals) {
				for (int i = 0; i < this.selectedVals.length; i++) {
					if (po.getOptValue().equals(this.selectedVals[i])) {
						sb.append(" checked='checked' ");
						break;
					}
				}
			}
			sb.append("value='" + po.getOptValue() + "' ");
			this.createBaseCss(sb, this.cssClass, this.styleClass);
			this.createEvent(sb);
			sb.append(">");
			if (this.txtPosition == 2) {
				sb.append(po.getOptName() + "  ");
			}
		}

		return sb.toString();

	}

	// private String createMultiWithUl(PropertisCacheTools cache) throws CacheClientException {
	private String createMultiWithUl(Map<String, Object> mapCache) {
		// List<PropertisOptions> pos = cache.getOptions(this.groupCode);
		List<PropertisOptions> pos = new ArrayList<PropertisOptions>();
		StringBuffer sb = new StringBuffer();
		sb.append("<ul ");
		this.createBaseCss(sb, this.ulcss, this.ulstyle);
		sb.append(">");
		for (PropertisOptions po : pos) {
			sb.append("<li ");
			this.createBaseCss(sb, this.licss, this.listyle);
			sb.append(">");
			if (this.txtPosition == 1) {
				sb.append(po.getOptName() + "  ");
			}
			sb.append("<input ");
			if (this.inputType.equals(INPUT_TYPE_RADIO)) {
				sb.append(" type='radio'");
			}
			if (this.inputType.equals(INPUT_TYPE_CHECKBOX)) {
				sb.append(" type='checkbox'");
			}
			sb.append(" id='" + this.inputId + "' name='" + this.inputName + "' ");
			if (null != selectedVals) {
				for (int i = 0; i < this.selectedVals.length; i++) {
					if (po.getOptValue().equals(this.selectedVals[i])) {
						sb.append(" checked='checked' ");
						break;
					}
				}
			}
			sb.append("value='" + po.getOptValue() + "' ");
			this.createEvent(sb);
			sb.append(">");
			if (this.txtPosition == 2) {
				sb.append(po.getOptName() + "  ");
			}
			sb.append("</li>");
		}
		sb.append("</ul>");
		return sb.toString();
	}

	// private String createText(PropertisCacheTools cache) throws CacheClientException {
	private String createText(Map<String, Object> mapCache) {
		// List<PropertisOptions> pos = cache.getOptions(this.groupCode);
		List<PropertisOptions> pos = new ArrayList<PropertisOptions>();
		StringBuffer sb = new StringBuffer();
		for (PropertisOptions po : pos) {
			if (null != selectedVals) {
				for (int i = 0; i < this.selectedVals.length; i++) {
					if (po.getOptValue().equals(this.selectedVals[i])) {
						sb.append(po.getOptName() + "  ");
						break;
					}
				}
			}
		}
		String inputTextHTML = "<input type='text' name='" + inputName + "' id='" + inputId + "' value='" + defaultValue + "' />";
		return inputTextHTML;
	}

	private void init() {
		if (StringUtils.isBlank(inputType)) {
			inputType = INPUT_TYPE_TEXT;
		}
		if (StringUtils.isBlank(inputId)) {
			inputId = groupCode;
		}
		if (StringUtils.isBlank(inputName)) {
			inputName = inputId;
		}
		if (inputType.equals(INPUT_TYPE_SELECT) && 1 == hasDefault) {
			if (StringUtils.isBlank(defaultName)) {
				defaultName = "----请选择----";
			}
		}
		if (StringUtils.isNotBlank(this.selectedValues)) {
			if (this.selectedValues.indexOf(",") != -1) {
				this.selectedVals = this.selectedValues.split(",");
			} else {
				this.selectedVals = new String[] { this.selectedValues };
			}
		}
		if (StringUtils.isBlank(this.cssClass)) {
			this.cssClass = "";
		}
		if (StringUtils.isBlank(this.styleClass)) {
			this.styleClass = "";
		}
		if (null == this.isul) {
			this.isul = 0;
		}
		if (1 == this.isul) {
			if (StringUtils.isBlank(this.ulcss)) {
				this.ulcss = this.cssClass;
			}
			if (StringUtils.isBlank(this.ulstyle)) {
				this.ulstyle = this.styleClass;
			}
		}
		if (StringUtils.isBlank(this.licss)) {
			this.licss = "";
		}
		if (StringUtils.isBlank(this.listyle)) {
			this.listyle = "";
		}
		if (null == this.txtPosition) {
			this.txtPosition = 1;
		}

	}

	private void createBaseCss(StringBuffer sb, String cssClass, String styleClass) {
		sb.append("class='" + cssClass + "' style='" + styleClass + "'");
	}

	private void createEvent(StringBuffer sb) {
		if (StringUtils.isNotBlank(onChange)) {
			sb.append(" " + EVENT_ON_CHANGE + "='" + onChange + "' ");
		}
		if (StringUtils.isNotBlank(onClick)) {
			sb.append(" " + EVENT_ON_CLICK + "='" + onClick + "' ");
		}
	}

	private void destroyParam() {
		groupCode = null;
		inputType = null;
		cssClass = null;
		selectedValues = null;
		selectedVals = null;
		inputId = null;
		inputName = null;
		defaultName = null;
		defaultValue = null;
		styleClass = null;
		isul = null;
		txtPosition = null;
	}

	public String getGroupCode() {
		return groupCode;
	}

	public void setGroupCode(String groupCode) {
		this.groupCode = groupCode;
	}

	public String getInputType() {
		return inputType;
	}

	public void setInputType(String inputType) {
		this.inputType = inputType;
	}

	public String getCssClass() {
		return cssClass;
	}

	public void setCssClass(String cssClass) {
		this.cssClass = cssClass;
	}

	public String getSelectedValues() {
		return selectedValues;
	}

	public void setSelectedValues(String selectedValues) {
		this.selectedValues = selectedValues;
	}

	public String getInputId() {
		return inputId;
	}

	public void setInputId(String inputId) {
		this.inputId = inputId;
	}

	public String getInputName() {
		return inputName;
	}

	public void setInputName(String inputName) {
		this.inputName = inputName;
	}

	public String getDefaultName() {
		return defaultName;
	}

	public void setDefaultName(String defaultName) {
		this.defaultName = defaultName;
	}

	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	public String getStyleClass() {
		return styleClass;
	}

	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}

	public String getOnChange() {
		return onChange;
	}

	public void setOnChange(String onChange) {
		this.onChange = onChange;
	}

	public String getOnClick() {
		return onClick;
	}

	public void setOnClick(String onClick) {
		this.onClick = onClick;
	}

	public Integer getHasDefault() {
		return hasDefault;
	}

	public void setHasDefault(Integer hasDefault) {
		this.hasDefault = hasDefault;
	}

	public Integer getIsul() {
		return isul;
	}

	public void setIsul(Integer isul) {
		this.isul = isul;
	}

	public String getUlcss() {
		return ulcss;
	}

	public void setUlcss(String ulcss) {
		this.ulcss = ulcss;
	}

	public String getUlstyle() {
		return ulstyle;
	}

	public void setUlstyle(String ulstyle) {
		this.ulstyle = ulstyle;
	}

	public String getLicss() {
		return licss;
	}

	public void setLicss(String licss) {
		this.licss = licss;
	}

	public String getListyle() {
		return listyle;
	}

	public void setListyle(String listyle) {
		this.listyle = listyle;
	}

	public Integer getTxtPosition() {
		return txtPosition;
	}

	public void setTxtPosition(Integer txtPosition) {
		this.txtPosition = txtPosition;
	}

}
